<?php
namespace core\models;
/**
 * 前台Mysqli数据库类
 * @package 数据库
 * @author ZhouHaoHua <53027327@qq.com> Administrator
 */
class Database_Mysqli extends Abstract_Database{
	private static $server, $username, $password, $database;
	protected function connect($server, $username, $password, $database){
        if(!$this->link){
			self::$server = $server;
			self::$username = $username;
			self::$password = $password;
			self::$database = $database;
			$this->link = new \mysqli(self::$server, self::$username, self::$password, self::$database);
            if ($this->link->connect_errno) {
                printf("Connect failed: %s\n", $this->link->connect_error);
                exit();
            }
            if(!$this->link->set_charset($this->charset)){
                printf("Error loading character set $this->charset: %s\n", $this->link->error);
                exit();
            }
		}
		return $this->link;
	}
	final public function close(){
        //这个关闭会出问题，暂时不要打开！
        /*if($this->link->thread_id) {
            $this->link->kill($this->link->thread_id);
            return $this->link->close();
        }*/
        return 0;
	}

	final public function query($query){
		if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
			error_log('QUERY ' . $query . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
		}

        $result = $this->link->query($query) or $this->error($query, $this->link->errno, $this->link->error);

		// 如果连接已断线则要重新连接，这种情况一般出现在使用长连接的情况下！
		if ($this->link->error && stripos($this->link->error, 'server has gone away') !== false) {
			$this->link = new \mysqli(self::$server, self::$username, self::$password, self::$database);
			if ($this->link->connect_errno) {
				printf("ReConnect failed: %s\n", $this->link->connect_error);
				exit();
			}
			if(!$this->link->set_charset($this->charset)){
				printf("Reset Error loading character set $this->charset: %s\n", $this->link->error);
				exit();
			}
			$result = $this->link->query($query) or $this->error($query, $this->link->errno, $this->link->error);
		}

		if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
			$result_error = $this->link->error;
			error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
		}
		return $result;
	}

	final public function fetch_array($db_query){
        return $db_query->fetch_array(MYSQLI_ASSOC);
	}

	final public function num_rows($db_query){
        return $db_query->num_rows;
	}

	final public function data_seek($db_query, $row_number){
        return $db_query->data_seek($row_number);
	}

	final public function insert_id(){
        return $this->link->insert_id;
	}

	final public function free_result($db_query) {
        return $db_query->free_result();
	}

	final public function fetch_fields($db_query) {
        return $db_query->fetch_field();
	}

	final public function input($string) {
		return $this->link->escape_string($string);
	}

	final public function affected_rows(){
		return $this->link->affected_rows;
	}

	public function __destruct(){
		return $this->close();
	}

    public function table_exists($table_name){
        $query = "SHOW TABLES LIKE '{$table_name}' ";

        $result = $this->link->query($query)->fetch_row();
        if($result && is_array($result)){
            return true;
        }else{
            return false;
        }
    }
}
